package com.czwj.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.czwj.annotation.SystemControllerLog;
import com.czwj.entities.BudgetItem;
import com.czwj.entities.DataGridData;
import com.czwj.entities.OperateResult;
import com.czwj.entities.PageBean;
import com.czwj.entities.UserInfo;
import com.czwj.service.BudgetItemService;
import com.czwj.util.TextUtil;

@Controller
public class BudgetItemAction {

	@Resource
	BudgetItemService budgetItemService;
	
	@RequestMapping(value = "/budgetitem",method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<BudgetItem> budget(HttpServletResponse response,
			@RequestParam(value = "page", required = false) String page,
			@RequestParam(value = "rows", required = false) String rows)  {
		DataGridData<BudgetItem> result = new DataGridData();
		Map<String, Object> map = new HashMap<String, Object>();
		PageBean pageBean = null;
		if (TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows))
		{
			pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
		}
		if (pageBean != null) {
			map.put("start", pageBean.getStart());
			map.put("size", pageBean.getPageSize());
		}
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		if(userInfo.getId()==1){
			
		}else{
			map.put("companyid",userInfo.getCompanyid());
		}
		List<BudgetItem> list = budgetItemService.search(map);
		result.setRows(list);
		result.setTotal(budgetItemService.count(map));
		return result;
	}
	
	@SystemControllerLog(description = "修改资金类别") 
	@RequestMapping(value = "/budgetItemSave.do", method = RequestMethod.POST)
	@ResponseBody
	public OperateResult budgetItemSave(@RequestBody BudgetItem item) throws Exception {
		OperateResult result = new OperateResult();
		int count = 0;
		if (item.getId() != null && item.getId() > 0) {
			count = budgetItemService.update(item);
		} else {
			Subject currentUser = SecurityUtils.getSubject();
			UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
			item.setCompanyid(userInfo.getCompanyid());
			count = budgetItemService.insert(item);
		}
		result.setSuccess(count > 0);
		return result;
	}

	@SystemControllerLog(description = "删除资金类别") 
	@RequestMapping(value = "budgetItemDelete.do", method = RequestMethod.POST)
	@ResponseBody
	public OperateResult budgetItemDelete(@RequestParam("budgetitemid") int id) {
		OperateResult result = new OperateResult();
		try {
			int count = budgetItemService.delete(id);
			result.setSuccess(count > 0);
			return result;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			result.setSuccess(false);
			return result;
		}
	}
	
	@RequestMapping(value = "/budgetItemlist.do", method = RequestMethod.POST)
	@ResponseBody
	public List<BudgetItem> budgetItemlist() {
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("companyid",userInfo.getCompanyid());
		List<BudgetItem> list = budgetItemService.search(map);
		return list;
	}
	
	@RequestMapping(value = "/existbudgetItemlist.do", method = RequestMethod.POST)
	@ResponseBody
	public List<BudgetItem> existbudgetItemlist(@RequestParam("projectitemid") int projectitemid) {
		List<BudgetItem> list = budgetItemService.existbudgetItemlist(projectitemid);
		return list;
	}

	@RequestMapping(value = "/existbudgetItemlistchild.do", method = RequestMethod.POST)
	@ResponseBody
	public List<BudgetItem> existbudgetItemlistchild(@RequestParam("projectitemchildid") int projectitemchildid) {
		List<BudgetItem> list = budgetItemService.existbudgetItemlistchild(projectitemchildid);
		return list;
	}
	
	@RequestMapping(value = "/check/budgetItemContent", method = RequestMethod.POST)
	@ResponseBody
	public OperateResult checkLoginname(@RequestParam(value = "content", required = true) String content,
			@RequestParam(value = "id", required = true) int id) {
		OperateResult result = new OperateResult();
		BudgetItem budgetItem = budgetItemService.findbycontent(content);
		if(budgetItem ==null){
			result.setSuccess(true);
		}else{
			if(budgetItem.getId()==id){
				result.setSuccess(true);
			}else
			{
				result.setSuccess(false);
			}
		}
		return result;
	}
	
	@RequestMapping(value = "/moneySituation", method = RequestMethod.GET)
	@ResponseBody
	public Map moneyposition(@RequestParam(value = "projectitemid") int projectitemid,
			@RequestParam(value = "itemid") int budgetitemid) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("projectitemid", projectitemid);
		map.put("budgetitemid", budgetitemid);
		Map<String, Double> result = budgetItemService.getMoneySituation(map);
		return result;
	}

	@RequestMapping(value = "/moneySituationChild", method = RequestMethod.GET)
	@ResponseBody
	public Map moneySituationChild(@RequestParam(value = "projectitemchildid") int projectitemchildid,
			@RequestParam(value = "itemid") int budgetitemid) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("projectitemchildid", projectitemchildid);
		map.put("budgetitemid", budgetitemid);
		Map<String, Double> result = budgetItemService.getMoneySituationChild(map);
		return result;
	}
}
